﻿/*
    2008.06.05 italki create, top ascx js functions
*/

//向子窗口传值变量
var WebIM = null;
var ToChildJid = null;

/*
    2008.05.26 italki create, get a dom object by id
    2008.06.05 italki update, copy from logic.js
*/
function $(obj)
{
    return document.getElementById(obj);
}

/*
    20008.06.06 italki create, get offline message
*/
function GetOfflineMessageCount(){

    var xmlhttp = CreateXMLHttpRequest();
    function GetStateCallBack()
    {
        if(xmlhttp.readyState == 4)
        {
            var xmldom = CreateXMLDocument();
            xmldom.loadXML(xmlhttp.responseText);
            var error = GetNodeValue(xmldom.selectSingleNode('/result/error'));
            if(error.length <= 0)
            {
                var msgcount =GetNodeValue(xmldom.selectSingleNode('/result/content'));
                
                if(msgcount != '0')
                {
                    $('img_chat').src = '/images/webim/icon_chat.gif';
                    $('img_chat').alt = 'check your italki Chat for new messages';
                    $('img_chat').title = 'check your italki Chat for new messages';
                    }
                    else
                    {
                        $('img_chat').src = '/images/webim/icon_nochat.gif';
                        $('img_chat').alt = 'italki Chat';
                        $('img_chat').title = 'italki Chat';
                    }
            }
        }
    }
    StartRequest(xmlhttp,'/webim/asyproxy.aspx?action=getofflinemsgcount&rand=' + Math.random(),'get',null,GetStateCallBack);
}

/*
    2008.06.03 italki create, get my state
    2008.06.05 italki update, move from logic.js
*/
function GetState()
{
    var xmlhttp = CreateXMLHttpRequest();
    function GetStateCallBack()
    {
        if(xmlhttp.readyState == 4)
        {
            var xmldom = CreateXMLDocument();
            xmldom.loadXML(xmlhttp.responseText);
            var error = GetNodeValue(xmldom.selectSingleNode('/result/error'));
            if(error.length <= 0)
            {
                var state =GetNodeValue(xmldom.selectSingleNode('/result/content'));
                switch(state)
                {
                    
                    case 'a':
                        $('img_state').src = '/images/webim/online.gif';
                        $('img_state').alt = 'available';
                        $('img_state').title = 'available';
                    break;
                    case 'b':
                        $('img_state').src = '/images/webim/busy.gif';
                        $('img_state').alt = 'busy';
                        $('img_state').title = 'busy';
                    break;
                    case 'c':
                        $('img_state').src = '/images/webim/offline.gif';
                        $('img_state').alt = 'invisible';
                        $('img_state').title = 'invisible';
                    break;
                }
            }
        }
    }
    StartRequest(xmlhttp,'/webim/asyproxy.aspx?action=getstate&rand=' + Math.random(),'get',null,GetStateCallBack);
}


/*
    2008.06.03 italki create, change state
    2008.06.05 italki update, move from logic.js
*/
function ChangeState(state){
    var xmlhttp = CreateXMLHttpRequest();
    function ChangeStateCallBack()
    {
        if(xmlhttp.readyState == 4)
        {
            var xmldom = CreateXMLDocument();
            xmldom.loadXML(xmlhttp.responseText);
            var error = GetNodeValue(xmldom.selectSingleNode('/result/error'));
            if(error.length <= 0)
            {
                $('div_changestate').style.display = 'none';
                switch(state)
                {
                    case 'a':
                        $('img_state').src = '/images/webim/online.gif';
                        $('img_state').alt = 'available';
                        $('img_state').title = 'available';
                    break;
                    case 'b':
                        $('img_state').src = '/images/webim/busy.gif';
                        $('img_state').alt = 'busy';
                        $('img_state').title = 'busy';
                    break;
                    case 'c':
                        $('img_state').src = '/images/webim/offline.gif';
                        $('img_state').alt = 'invisible';
                        $('img_state').title = 'invisible';
                    break;
                }
            }
        }
    }
    StartRequest(xmlhttp,'/webim/asyproxy.aspx?action=setstate&state=' + state + '&rand=' + Math.random(),'get',null,ChangeStateCallBack);
}


/*
    2008.06.16 snail create, float div
*/
function ShowFloatDiv(sender){
    $('div_changestate').style.top = getAbsoluteOffsetTop(sender) + 13;
    $('div_changestate').style.left = getAbsoluteOffsetLeft(sender) - 70;
    $('div_changestate').style.display = '';
}

function HideFloatDiv()
{
    $('div_changestate').style.display = 'none';
}

function getAbsoluteOffsetTop(obj) {
    var y = obj.offsetTop;
    while (obj = obj.offsetParent) y += obj.offsetTop;
    return y;
}

function getAbsoluteOffsetLeft(obj) {
    var x = obj.offsetLeft;
    while (obj = obj.offsetParent) x += obj.offsetLeft;
    return x;
}
